package dao;

import beans.User;
import cn.hutool.crypto.digest.DigestUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.JDBCUtils;

import java.sql.SQLException;

/**
 * 专门用来操作数据库
 */
public class UserDao {
    /**
     *
     * @param user service层传递过来的用户信息
     * @return 查询的结果,如果用户名密码正确,返回用户对象,否则返回null
     */
    public User login(User user) {
       //查询数据库
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        User existUser = null;
        try {
            existUser = qr.query("SELECT * FROM `user` WHERE username=? AND PASSWORD= ?",
                    new BeanHandler<User>(User.class),
                    user.getUsername(), DigestUtil.md5Hex(user.getPassword()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return existUser;
    }
}
